package com.yunfeisoft.utils;

import com.yunfeisoft.base.dao.inter.*;
import com.yunfeisoft.base.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 工作流帮助类
 */
public class WorkflowHelper {

    private static Logger logger = LoggerFactory.getLogger(WorkflowHelper.class);

    @Autowired
    private WorkflowStepDao workflowStepDao;
    @Autowired
    private WorkflowInstanceDao workflowInstanceDao;
    @Autowired
    private WorkflowHistoryDao workflowHistoryDao;
    @Autowired
    private WorkflowTaskDao workflowTaskDao;
    @Autowired
    private WorkflowStepUserDao workflowStepUserDao;

    /**
     * 保存并启动流程实例
     *
     * @param instance 流程实例
     * @return
     */
    public void saveAndStartProcess(WorkflowInstance instance, String executor) {
        //1 保存流程实例
        instance.setStatus(WorkflowInstance.WorkflowInstanceStatusEnum.APPROVALING.getValue());
        workflowInstanceDao.insert(instance);

        //2 获取下一执行步骤
        WorkflowStep step = getNextStep(instance.getWorkflowDefineId(), 0);

        //3 保存下一步执行任务
        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setWorkflowInstanceId(instance.getId());
        workflowTask.setWorkflowStepId(step.getId());
        workflowTask.setStepName(step.getName());
        workflowTask.setStepNum(step.getStepNum());
        workflowTask.setExecutor(executor);
        workflowTask.setStatus(WorkflowInstance.WorkflowInstanceStatusEnum.APPROVALING.getValue());
        workflowTaskDao.insert(workflowTask);
    }

    /**
     * 重新提交流程实例
     *
     * @param processInstanceId 流程实例id
     * @param executor          下一步任务执行人id
     * @return
     */
    public void submitProcess(String processInstanceId, String executor) {
        WorkflowInstance instance = workflowInstanceDao.load(processInstanceId);
        //1 保存流程实例
        instance.setStatus(WorkflowInstance.WorkflowInstanceStatusEnum.APPROVALING.getValue());
        workflowInstanceDao.update(instance);

        //2 获取下一执行步骤
        WorkflowStep step = getNextStep(instance.getWorkflowDefineId(), 0);

        //3 保存下一步执行任务
        WorkflowTask workflowTask = new WorkflowTask();
        workflowTask.setWorkflowInstanceId(instance.getId());
        workflowTask.setWorkflowStepId(step.getId());
        workflowTask.setStepName(step.getName());
        workflowTask.setStepNum(step.getStepNum());
        workflowTask.setExecutor(executor);
        workflowTask.setStatus(WorkflowInstance.WorkflowInstanceStatusEnum.APPROVALING.getValue());
        workflowTaskDao.insert(workflowTask);
    }

    /**
     * 执行任务
     *
     * @param taskId   任务id
     * @param status   状态
     * @param remark   执行结果
     * @param executor 执行人
     */
    public void executeTask(String taskId, int status, String remark, String executor) {
        WorkflowTask task = workflowTaskDao.load(taskId);
        task.setStatus(status);
        task.setRemark(remark);

        //1 删除当前执行任务
        workflowTaskDao.remove(taskId);

        //2 把当前任务移到历史任务表
        WorkflowHistory history = task.toHistory();
        workflowHistoryDao.insert(history);

        //3 判断执行状态
        if (status == WorkflowInstance.WorkflowInstanceStatusEnum.ACCEPT.getValue()) {//通过
            WorkflowInstance instance = workflowInstanceDao.load(task.getWorkflowInstanceId());
            WorkflowStep nextStep = getNextStep(instance.getWorkflowDefineId(), task.getStepNum());
            if (nextStep == null) {//流程执行结束，设置流程实例状态为审批通过
                instance.setStatus(WorkflowInstance.WorkflowInstanceStatusEnum.ACCEPT.getValue());
                workflowInstanceDao.update(instance);
                return;
            }

            //保存下一步执行任务
            WorkflowTask workflowTask = new WorkflowTask();
            workflowTask.setWorkflowInstanceId(instance.getId());
            workflowTask.setWorkflowStepId(nextStep.getId());
            workflowTask.setStepName(nextStep.getName());
            workflowTask.setStepNum(nextStep.getStepNum());
            workflowTask.setExecutor(executor);
            workflowTask.setStatus(WorkflowInstance.WorkflowInstanceStatusEnum.APPROVALING.getValue());
            workflowTaskDao.insert(workflowTask);
            return;
        }

        if (status == WorkflowInstance.WorkflowInstanceStatusEnum.REJECT.getValue()) {//驳回，设置流程实例状态为审批驳回
            WorkflowInstance instance = new WorkflowInstance();
            instance.setId(task.getWorkflowInstanceId());
            instance.setStatus(WorkflowInstance.WorkflowInstanceStatusEnum.REJECT.getValue());
            workflowInstanceDao.update(instance);
            return;
        }
    }

    /**
     * 获取下一个步骤
     *
     * @param workflowDefineId 流程定义id
     * @param currentStepNum   当前执行步骤顺序号
     * @return
     */
    public WorkflowStep getNextStep(String workflowDefineId, int currentStepNum) {
        return workflowStepDao.getNextStep(workflowDefineId, currentStepNum);
    }

    /**
     * 获取下一个步骤执行人
     *
     * @param workflowDefineId 流程定义id
     * @param currentStepNum   当前执行步骤顺序号
     * @return
     */
    public List<WorkflowStepUser> getNextStepExecutor(String workflowDefineId, int currentStepNum) {
        WorkflowStep nextStep = getNextStep(workflowDefineId, currentStepNum);
        if (nextStep == null) {
            return null;
        }
        return workflowStepUserDao.queryByWorkflowStepId(nextStep.getId());
    }
}
